package com.swlz.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swlz.commons.DBUtil;
import java.util.Date;
import java.util.List;
import java.sql.SQLException;
import com.swlz.dao.MallOrderDAO;
import com.swlz.dao.MallTypeDAO;
import com.swlz.dto.MallSearch;
import com.swlz.dto.OrderSearch;
import com.swlz.entity.MallOrder;
import com.swlz.entity.MallType;
import com.swlz.exception.CodeException;
import com.swlz.service.MallOrderService;
import com.swlz.vo.OrderVO;
import com.swlz.vo.PageVO;
import com.swlz.vo.ProductVo;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 商城订单
 * ClassName : com.swlz.service.impl.MallOrderServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-02 11:12:39
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallOrderServiceImpl implements MallOrderService {
	private static Logger log = LoggerFactory.getLogger(MallOrderServiceImpl.class);
	
	private MallOrderDAO mallOrderDAO = new MallOrderDAO();

	@Override
	public int add(MallOrder mallOrder) throws CodeException {
		try {
			return mallOrderDAO.insertSelective(mallOrder);
		} catch (SQLException e) {
			log.warn("添加商城订单错误",e);
			throw new CodeException(500, "添加商城订单错误",e);
		}
	}

	@Override
	public int update(MallOrder mallOrder) throws CodeException {
		try {
			return mallOrderDAO.updateSelective(mallOrder);
		} catch (SQLException e) {
			log.warn("修改商城订单错误",e);
			throw new CodeException(501, "修改商城订单错误",e);
		}
	}

	@Override
	public int delete(Long orderId) throws CodeException {
		try {
			return mallOrderDAO.deleteById(orderId);
		} catch (SQLException e) {
			log.warn("删除商城订单错误",e);
			throw new CodeException(502, "删除商城订单错误",e);
		}
	}

	@Override
	public MallOrder get(Long orderId) throws CodeException {
		try {
			return mallOrderDAO.selectById(orderId);
		} catch (SQLException e) {
			log.warn("获取商城订单失败",e);
			throw new CodeException(503, "获取商城订单失败",e);
		}
	}
	
	
		
	@Override
	public PageVO<MallOrder> page(MallOrder mallOrder, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = mallOrderDAO.countByModel(mallOrder);
			PageVO<MallOrder> page = new PageVO<>(total, current, rowMax);
			return page.setList(mallOrderDAO.selectByModel(mallOrder, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			log.warn("获取商城订单分页数据失败",e);
			throw new CodeException(504, "获取商城订单分页数据失败",e);
		}
	}
	
	@Override
	public PageVO<OrderVO> pages(OrderSearch ordersearch) throws CodeException {
		try {
			PageVO<OrderVO> page = new PageVO<OrderVO>(MallOrderDAO.count(ordersearch), ordersearch.getPageNumber(),
					ordersearch.getPageSize());
			return page.setList(MallOrderDAO.select(ordersearch, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			e.printStackTrace();// 日志
			throw new RuntimeException("获取商城订单分页数据失败", e);
		}

		
		
		
	}

	@Override
	public List<MallOrder> list() {
		try {
			return MallOrderDAO.selectByModel(null, 0, 99);
		} catch (SQLException e) {
			e.printStackTrace();//
			throw new RuntimeException("获取商品类别失败", e);
		}
	}


	
	
	
	@Override
	public List<MallOrder> list(MallOrder mallOrder, int len) {
		try {
			return mallOrderDAO.selectByModel(mallOrder, 0, len);
		} catch (SQLException e) {
			log.warn("获取商城订单列表数据失败",e);
			throw new CodeException(505, "获取商城订单列表数据失败",e);
		}	
	}

	@Override
	public void confirmPay(String out_trade_no, String trade_no, String total_amount) {
		MallOrder order = null;
		try{
			order = mallOrderDAO.selectById(Long.valueOf(out_trade_no));
		}catch(Exception ex){
			throw new CodeException(1000, "获取原订单失败！");
		}
		Float payMoney = null;
		try{
			payMoney = Float.valueOf(total_amount);
		}catch(Exception ex){
			throw new CodeException(1001, "获取实际支付的金额失败！");
		}
		
		if(!payMoney.equals(order.getOrderMoney())){
			throw new CodeException(1002,"实际支付的金额不符合");
		}
		
		order.setOrderPayId(trade_no);
		order.setOrderPayTime(new Date());
		order.setOrderPayTypeId(1);
		order.setOrderStatus(1);
		try {
			mallOrderDAO.update(order);
		} catch (SQLException e) {
			throw new CodeException(1003,"记录订单支付状态失败");
		}
	}

}
